<?php
/**
 * Class that operate on table 'partner'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-05-22 23:17
 */
class PartnerMySqlDAO implements PartnerDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return PartnerMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM partner WHERE PKPartner = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM partner';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM partner ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param partner primary key
 	 */
	public function delete($PKPartner){
		$sql = 'DELETE FROM partner WHERE PKPartner = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($PKPartner);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param PartnerMySql partner
 	 */
	public function insert($partner){
		$sql = 'INSERT INTO partner (FKTargetType_Person, Name, Birthdate, MeansContact, MeansIdentification, Gender, Active, Visible) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($partner->fKTargetTypePerson);
		$sqlQuery->set($partner->name);
		$sqlQuery->set($partner->birthdate);
		$sqlQuery->set($partner->meansContact);
		$sqlQuery->set($partner->meansIdentification);
		$sqlQuery->setNumber($partner->gender);
		$sqlQuery->setNumber($partner->active);
		$sqlQuery->setNumber($partner->visible);

		$id = $this->executeInsert($sqlQuery);	
		$partner->pKPartner = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param PartnerMySql partner
 	 */
	public function update($partner){
		$sql = 'UPDATE partner SET FKTargetType_Person = ?, Name = ?, Birthdate = ?, MeansContact = ?, MeansIdentification = ?, Gender = ?, Active = ?, Visible = ? WHERE PKPartner = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($partner->fKTargetTypePerson);
		$sqlQuery->set($partner->name);
		$sqlQuery->set($partner->birthdate);
		$sqlQuery->set($partner->meansContact);
		$sqlQuery->set($partner->meansIdentification);
		$sqlQuery->setNumber($partner->gender);
		$sqlQuery->setNumber($partner->active);
		$sqlQuery->setNumber($partner->visible);
		$sqlQuery->setNumber($partner->pKPartner);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM partner';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByFKTargetTypePerson($value){
		$sql = 'SELECT * FROM partner WHERE FKTargetType_Person = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByName($value){
		$sql = 'SELECT * FROM partner WHERE Name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByBirthdate($value){
		$sql = 'SELECT * FROM partner WHERE Birthdate = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMeansContact($value){
		$sql = 'SELECT * FROM partner WHERE MeansContact = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMeansIdentification($value){
		$sql = 'SELECT * FROM partner WHERE MeansIdentification = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByGender($value){
		$sql = 'SELECT * FROM partner WHERE Gender = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByActive($value){
		$sql = 'SELECT * FROM partner WHERE Active = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByVisible($value){
		$sql = 'SELECT * FROM partner WHERE Visible = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByFKTargetTypePerson($value){
		$sql = 'DELETE FROM partner WHERE FKTargetType_Person = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByName($value){
		$sql = 'DELETE FROM partner WHERE Name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByBirthdate($value){
		$sql = 'DELETE FROM partner WHERE Birthdate = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMeansContact($value){
		$sql = 'DELETE FROM partner WHERE MeansContact = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMeansIdentification($value){
		$sql = 'DELETE FROM partner WHERE MeansIdentification = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByGender($value){
		$sql = 'DELETE FROM partner WHERE Gender = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByActive($value){
		$sql = 'DELETE FROM partner WHERE Active = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByVisible($value){
		$sql = 'DELETE FROM partner WHERE Visible = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return PartnerMySql 
	 */
	protected function readRow($row){
		$partner = new Partner();
		
		$partner->pKPartner = $row['PKPartner'];
		$partner->fKTargetTypePerson = $row['FKTargetType_Person'];
		$partner->name = $row['Name'];
		$partner->birthdate = $row['Birthdate'];
		$partner->meansContact = $row['MeansContact'];
		$partner->meansIdentification = $row['MeansIdentification'];
		$partner->gender = $row['Gender'];
		$partner->active = $row['Active'];
		$partner->visible = $row['Visible'];

		return $partner;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return PartnerMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>